package com.panda.pandawash.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.panda.pandawash.R;
import com.panda.pandawash.api.ApplicationConstant;

/**
 * Created by gaowf on 2017/3/23.
 */

public class PlateConfirmActivity extends AppCompatActivity {

    // 编辑车牌号
    private TextView licensePlate;
    private String licensePlateName;

    //已洗车几次的布局
    private TextView washCarTimes;

    //收费洗车、免费洗车、退出按钮
    private Button freeWashBtn;
    private Button feeWashBtn;
    private Button quitBtn;

    private ImageView backBtn;

    private String mPlateName;
    private int washTime;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_plate_info);

        licensePlate = (TextView) findViewById(R.id.edit_license_plate_confirm);
        washCarTimes = (TextView) findViewById(R.id.wash_car_times);//洗车次数

        ///免费，收费，退出
        feeWashBtn = (Button) findViewById(R.id.btn_fee_wash_car);
        feeWashBtn.setOnClickListener(mOnClickListener);
        freeWashBtn = (Button) findViewById(R.id.btn_free_wash_car);
        freeWashBtn.setOnClickListener(mOnClickListener);
        quitBtn = (Button) findViewById(R.id.quit_license_confirm);
        quitBtn.setOnClickListener(mOnClickListener);

        //返回
        backBtn = (ImageView) findViewById(R.id.btn_return_license_confirm);
        backBtn.setOnClickListener(mOnClickListener);
        initRes();

    }

    private void initRes() {
        mPlateName = getIntent().getStringExtra("platename");
        licensePlate.setText(mPlateName);
    }



    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btn_fee_wash_car:
                    confirmFeeWash();
                    /**
                     * 通知
                     */
                    break;
                case R.id.btn_free_wash_car:
                    // 免费洗车
                    Intent intent = new Intent(PlateConfirmActivity.this,FreeWashReasonActivity.class);
                    startActivityForResult(intent, ApplicationConstant.FREE_WASH_REASON_SETTINGS);
                    break;
                case R.id.quit_license_confirm:
                    finish();
                    break;

                case R.id.btn_return_license_confirm:
                    finish();
                    break;
            }
        }
    };

    private void confirmFeeWash() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(PlateConfirmActivity.this);
        builder.setMessage(getResources().getString(R.string.confirm_fee_wash_car_dialog));
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                /**
                 * 当洗车次数达到10的整数倍的时候，优惠服务，其余洗车次数加1
                 */
                Log.d("hys123","washTime"+washCarTimes.getText().toString());
                washTime= Integer.parseInt(washCarTimes.getText().toString());
                if(washTime>0 && washTime%10 ==0){
                    requestDialog(washTime);
                }
                washTime ++;
                washCarTimes.setText(washTime+"");
                dialogInterface.dismiss();
            }
        });
        builder.show();
    }

    private void requestDialog(int washTime) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(PlateConfirmActivity.this);
        builder.setMessage(getResources().getString(R.string.car_wash_promotions_notification,washTime).toString());
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        builder.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {
            /// 返回的原因需要处理并发送
            case ApplicationConstant.FREE_WASH_REASON_SETTINGS:
                String freeReason = data.getStringExtra("free_reason");
                if(freeReason != null) {
                    washTime = Integer.parseInt(washCarTimes.getText().toString());
                    washTime++;
                    washCarTimes.setText(washTime + "");
                }
                break;
            default:
                break;
        }
    }

}
